{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    x1   x2  y\n",
      "0  7.0  4.7  1\n",
      "1  6.4  4.5  1\n",
      "2  6.9  4.9  1\n",
      "3  5.5  4.0  1\n",
      "4  6.5  4.6  1\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>5.471000</td>\n",
       "      <td>2.861000</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.641698</td>\n",
       "      <td>1.449549</td>\n",
       "      <td>0.502519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>4.300000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>5.000000</td>\n",
       "      <td>1.500000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>5.400000</td>\n",
       "      <td>2.450000</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>5.900000</td>\n",
       "      <td>4.325000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>7.000000</td>\n",
       "      <td>5.100000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               x1          x2           y\n",
       "count  100.000000  100.000000  100.000000\n",
       "mean     5.471000    2.861000    0.500000\n",
       "std      0.641698    1.449549    0.502519\n",
       "min      4.300000    1.000000    0.000000\n",
       "25%      5.000000    1.500000    0.000000\n",
       "50%      5.400000    2.450000    0.500000\n",
       "75%      5.900000    4.325000    1.000000\n",
       "max      7.000000    5.100000    1.000000"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "#dataframe   转 numpy\n",
    "#data = pd.read_csv('data.csv', names=['x1', 'x2', 'y']).values\n",
    "data = pd.read_csv('data.csv', names=['x1', 'x2', 'y'])\n",
    "print(data.head())\n",
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYkAAAFYCAYAAACmvwA3AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvDW2N/gAAIABJREFUeJzt3Xt0VPXV//HPZCY3iIgGiAqCSKMuwKBWiYr4yE/Eyw+EllJui8r6KQpdYhFEZXmlctGFUhSqIkurKD4UeCy0Ilqp9qkFJbQKLakuAlYiVyEQIJDMJ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OC4a/AfCyhN9JPProo4leMq4att5KUpuAT6qJ6IV/VnA3ASDpufaX6UpKSpwOIYpZPDuPZKldwFCwQadtmiHtPOJzXQ513BpXS3klD8k7uXglDym2XPLz822MxFmuLRJuusi1v6XceDw9dhzUgcpw/Z2EVNth1eMMvyt/s7mpXJKJV/KQvJOLV/KQvJVLrDzTAusUWm8BeBlFIka03gLwMsfeblqzZo1TS9uO4W8AvIo7CQCAKYoEAMAURQIAYIoiAQAwRZEAAJiiSAAATFEkAACmfOXl5UbzTwMApCLuJAAApigSAABTFAkAgCmKBADAlGv3k3DS888/r/Lycj3xxBNRx9999139+te/1tlnny1J6tevn37+8587EWKTJk2apMOHDysQqH15Z8yYod69e9c/XlRUpAULFigYDGrgwIGaNGmSU6E2q7lcfvnLX2rr1q3KysqSJN11110aMGCAI7E25ZNPPtGSJUtUVVWlwsJCTZs2Lerx7du3a9asWTpx4oQuv/xyPfzww/U5u01zuSxZskR/+MMfdMYZZ0iShg0bphEjRjgRqqnVq1dr5cqV9V/v3btXt912m6ZPn15/LJlek3hKvYybUVRUpLVr16pfv36nPfbll19qypQpuvnmmx2IzBrDMFRaWqo1a9Y0+h90VVWVnnrqKb388svKy8vT/fffr40bN+raa691INqmNZeLVPuaLF68WB06dEhwdNbt2bNHTz/9tF577TXl5uZq0qRJp13zxx9/XI888oguvfRSPfXUU1q9erV+8pOfOBh146zk8uWXX2rWrFkqKChwMNKmDRs2TMOGDZMk7dy5Uw8++KAmTJgQ9ZxkeU3ijbebGjh69KhefvlljR8/vtHHv/zyS61du1ajR4/W448/rmPHjiU2QAt27dolSZo8ebLGjBmjFStWRD3+r380HyreAAAF/klEQVT9S+eff746d+6sQCCgW2+9VX/605+cCLVZzeVSVVWl/fv366mnntKYMWP0yiuvKBKJOBFqk/785z9r4MCBysvLUyAQ0Jw5c9SrV6/6x/ft26dgMKhLL71UkjR48GDXvibN5SLV/j15/fXXNWbMGM2bN0/BYNChaK155plnNGnSJLVv377+WDK9JvFGkWhg7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sd+/eGjduXH2chYWFGjJkiKZMmaK7775bPXv21C9/+UvNmTNHJ06c0MUXX6yRI0c6HXajrOQyY8YMTZs2TdXV1brssss0duxYp8Nu1J49e9SpU6eoY8n4msQbA/4AAKZ4uwkAYIoiAQAwRZEAAJiiSAAATFEkAACmKBJIaTNmzNDjjz/udBiAa1EkkJIikYjmz5+fskPbAKv4ZTqknB07dujZZ59VcXGxMjMznQ4HcDXuJOApn3zyifr27asXX3yx/tj+/fs1YMAAPfDAA5KkJ554QuFwWK+//nr9BlIAGsedBDylf//+GjJkiN566y0NGjRIPXr00MyZM5Wdna1HH31UkvTkk08qPz/f4UiB5ECRgOfcf//92rx5s+bOnauBAwfq888/1wsvvFC/qQwFArCOIgHPycnJ0WOPPaZ7771XxcXFGj16tAoLC50OC0hKfCYBT7r88svVuXNnRSKRRvcLAGANRQKe9MYbb2jfvn268MILNXv2bFVVVTkdEpCUKBLwnO3bt+vVV1/VuHHjNHfuXO3fv18LFy50OiwgKVEk4CmhUEhPPPGEOnfurLvuukvdu3fX+PHjtWrVKm3evNnp8ICkQ5GApyxevFhff/21HnnkEWVkZEiSxo8fr+7du+upp55SRUWFwxECyYWd6QAApriTAACYokgAAExRJAAApigSAABTFAkAgCmKBADAFEUCAGCKIgEAMEWRAACY+v8xfkji/Yqp+AAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 405.725x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "plt.style.use('fivethirtyeight')\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "sns.lmplot('x1', 'x2', hue='y', data=data, fit_reg=False)\n",
    "plt.show()#看下数据的样子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'data' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-705e725b8f8d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m#x = np.loadtxt(\"data.csv\",delimiter=\",\",usecols=(0,1))\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;31m#y = np.loadtxt(\"data.csv\",delimiter=\",\",usecols=(2,))\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'data' is not defined"
     ]
    }
   ],
   "source": [
    "#import numpy as np\n",
    "#x = np.loadtxt(\"data.csv\",delimiter=\",\",usecols=(0,1))\n",
    "#y = np.loadtxt(\"data.csv\",delimiter=\",\",usecols=(2,))\n",
    "x=data.iloc[:,[0,1]].values\n",
    "y=data.iloc[:,[2,]].values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 准备训练测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "#把数据集分为训练集和测试集\n",
    "#train_data：被划分的样本特征集\n",
    "#train_target：被划分的样本标签\n",
    "#test_size：如果是浮点数，在0-1之间，表示测试样本占比；如果是整数的话就是样本的数量\n",
    "#random_state：是随机数的种子，就是该组随机数的编号，在需要重复试验的时候，保证得到一组一样的随机数。\n",
    "X_train,X_test,y_train,y_test = train_test_split(x, y,test_size=0.25,random_state=42)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用逻辑回归进行预测任务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.externals import joblib\n",
    "\n",
    "# 标准化数据，保证每个维度的特征数据方差为1，均值为0。使得预测结果不会被某些维度过大的特征值而主导\n",
    "ss = StandardScaler()\n",
    "\n",
    "#fit 是为计算该类处理所需的相关参数，以标准化为例，fit()就是计算标准化所用到的均值与方差\n",
    "#transform：利用fit（）的结果作为参数对数据进行相应的处理，比如正规化。\n",
    "#fit_transform :可以看做是fit和transform的结合，如果训练阶段使用fit_transform，则在测试阶段只需要对测试样本进行transform就行了。\n",
    "X_train = ss.fit_transform(X_train)\n",
    "X_test = ss.transform(X_test)\n",
    "#print(X_train)\n",
    "#print(X_test)\n",
    "# 初始化 LogisticRegression\n",
    " \n",
    "lr = LogisticRegression(C=1.0, penalty='l1', tol=0.04, solver = 'liblinear')\n",
    " \n",
    "# 跳用LogisticRegression中的fit函数／模块来训练模型参数\n",
    "lr.fit(X_train, y_train.ravel())\n",
    "\n",
    "\n",
    "#保存模型\n",
    "joblib.dump(lr, \"5_sk_lr.m\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "#加载模型\n",
    "n_lr = joblib.load(\"5_sk_lr.m\")\n",
    "\n",
    "\n",
    "lr_y_predict = n_lr.predict(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 性能分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "score： 1.0\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         die       1.00      1.00      1.00        11\n",
      "       alive       1.00      1.00      1.00        14\n",
      "\n",
      "   micro avg       1.00      1.00      1.00        25\n",
      "   macro avg       1.00      1.00      1.00        25\n",
      "weighted avg       1.00      1.00      1.00        25\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report\n",
    " \n",
    "# 利用逻辑斯蒂回归自带的评分函数score获得模型在测试集上的准确定结果\n",
    "print ('score：',lr.score(X_test,y_test))\n",
    " \n",
    "print (classification_report(y_test,lr_y_predict,target_names = ['die','alive']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
